<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>03-1监视属性-立即监视-immediate</title>
        <!-- 引入Vue -->
        <script type="text/javascript" src="../js/vue.js"></script>
    </head>
    <body>
        <!-- 准备好一个容器-->
        <div id="demo">
            姓: <input type="text" v-model="fristName"><br>
            名: <input type="text" v-model="lastName"><br>
            全名: <span>{{fullName}}</span><br>
            <button @click = "changeFullName">点击切换li-si</button><br>
            <img :src="url" width = "100px">
        </div>
    </body>

    <script type="text/javascript">
        //阻止 vue 在启动时生成生产提示。
        Vue.config.productionTip = false
       const vm = new Vue({
            el:'#demo',
            data:{
                fristName:'zhang',
                lastName:'san',
                url:''
            },
            computed:{
                fullName:{
                    get(){
                        return this.fristName + '-' +this.lastName;
                      
                    },
                    set(val){
                        const arr = val.split('-');   //拆分字符串  并形成新的数组
                        this.fristName = arr[0];
                        this.lastName = arr[1];
                    }
                   
                }
            },
            methods:{
                changeFullName(){
                    this.fullName = 'li-si' 
                    
                }
            },
            watch:{
                fullName:{
                    // immediate:true,
                    handler(val){
                        if(val) this.url = 'https://s3.ax1x.com/2020/12/13/reC1IA.jpg' 
                        else this.url = 'https://s3.ax1x.com/2020/12/13/reCaqg.jpg'
                        
                    }
                }
            }

        })
        console.log(vm)
    </script>
</html>